﻿using ArtMath.Core.Helper;
using ArtMath.Resource;
using ArtMath.Setting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using ArtMath.Flag;
using System.ComponentModel;
using System.Timers;
using ArtMath.Control;
using ArtMath.CommonType;

namespace ArtMath.Window
{
    public partial class DocumentsManagerWindow : DockContent
    {
        [Browsable(false)]
        public Document LinkedDocument { get; private set; }
        System.Timers.Timer autoSave = new System.Timers.Timer((Settings.Instance.AutoSaveInterval == 0 ? 300 : Settings.Instance.AutoSaveInterval) * 1000),
            changeInvertionState = new System.Timers.Timer(Settings.Instance.FlashInterval == 0 ? 700 : Settings.Instance.FlashInterval);
        HashSet<SketchPad> containsSelectedItems = new HashSet<SketchPad>();
        [Browsable(false)]
        public System.Timers.Timer AutoSave
        {
            get { return autoSave; }
        }
        [Browsable(false)]
        public System.Timers.Timer ChangeInvertionState
        {
            get { return changeInvertionState; }
        }
        [Browsable(false)]
        public bool HasSelectedItems
        {
            get { return containsSelectedItems.Count > 0; }
        }
        public void SketchPad_Layer_RefreshWindowsStatus(LayerControl sender, LayerControl.RefreshWindowsStatusEventArgs e)
        {
            if (sender.SelectedItems.Count == 0)
            {
                containsSelectedItems.Remove(sender.Pad);
                if (containsSelectedItems.Count == 0)
                {
                    if (ChangeInvertionState.Enabled)
                    {
                        ChangeInvertionState.Stop();
                    }
                }
            }
            else
            {
                containsSelectedItems.Add(sender.Pad);
                if (!ChangeInvertionState.Enabled && Settings.Instance.FlashInterval > 0)
                {
                    ChangeInvertionState.Start();
                }
            }
        }
        public void Document_Activated(Document sender)
        {
            foreach (ListViewItem item in lvDocuments.SelectedItems)
            {
                item.Selected = false;
            }
            LinkedDocument = sender;
            if (LinkedDocument == null)
            {

            }
            else
            {
                LinkedDocument.DocumentsManagerItem.Selected = true;
            }
        }
        public Document CreateDocument(string title)
        {
            Document doc = new Document(title);
            doc.RegisterDocumentEvents();
            lvDocuments.Items.Add(doc.DocumentsManagerItem);
            doc.Activate();
            return doc;
        }
        public void Document_DocumentWindow_FloatAll_Click(object sender, EventArgs e)
        {
            Float(true);
        }
        public void Document_DocumentWindow_Save_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem menu = sender as ToolStripMenuItem;
            Save(new Document[] { menu.Tag as Document });
        }
        public void Document_DocumentWindow_Close_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem menu = sender as ToolStripMenuItem;
            try
            {
                CloseDocument(new Document[] { menu.Tag as Document });
            }
            catch (Exception)
            {

            }
        }
        public void Document_DocumentWindow_CloseOther_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem menu = sender as ToolStripMenuItem;
            HashSet<Document> docs = new HashSet<Document>(lvDocuments.GetItems(true).Select((ListViewItem item) => { return item.Tag as Document; }));
            docs.Remove(menu.Tag as Document);
            try
            {
                CloseDocument(docs);
            }
            catch (Exception)
            {

            }
        }
        public void Document_DocumentWindow_CloseAll_Click(object sender, EventArgs e)
        {
            try
            {
                CloseDocument(true);
            }
            catch (Exception)
            {

            }
        }
        public void Document_DocumentWindow_CloseLeft_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem menu = sender as ToolStripMenuItem;
            IDockContent[] dc = Program.WinMain.MainDockPanel.DocumentsToArray();
            List<Document> docs = new List<Document>();
            int index = Array.IndexOf(dc, menu.Tag as DocumentWindow);
            for (int i = 0; i < index; i++)
            {
                docs.Add((dc[i] as DocumentWindow).LinkedDocument);
            }
            try
            {
                CloseDocument(docs);
            }
            catch (Exception)
            {

            }
        }
        public void Document_DocumentWindow_CloseRight_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem menu = sender as ToolStripMenuItem;
            IDockContent[] dc = Program.WinMain.MainDockPanel.DocumentsToArray();
            List<Document> docs = new List<Document>();
            int index = Array.IndexOf(dc, menu.Tag as DocumentWindow);
            for (int i = index + 1; i < dc.Length; i++)
            {
                docs.Add((dc[i] as DocumentWindow).LinkedDocument);
            }
            try
            {
                CloseDocument(docs);
            }
            catch (Exception)
            {

            }
        }
        public void Document_AddSketchpadButton_Click(object sender, EventArgs e)
        {
            AddSketchpad(new Document[] { (sender as Button).Tag as Document });
        }
        public void Document_TitleChanged(Document sender)
        {
            if (sender.DocumentsManagerItem.Selected && sender.DocumentsManagerItem.ListView.SelectedItems.Count == 1)
            {
                Text = $"{Resources.JoinLocalizedString("Document", "Manager")} - {sender.Title}{(sender.Modified ? "*" : string.Empty)}";
            }
        }
        public DocumentsManagerWindow()
        {
            InitializeComponent();
            ofd.Filter = sfd.Filter = $"{Flags.SoftwareName + Resources.Join + Resources.Translate("Document")}(*{Flags.DocumentExtension})|*{Flags.DocumentExtension}";
            ofd.InitialDirectory = sfd.InitialDirectory = Flags.DesktopPath;
            ChangeColorTheme(Settings.Instance.ColorTheme);
            AutoSave.Elapsed += AutoSave_Elapsed;
            ChangeInvertionState.Elapsed += ChangeInvertionState_Elapsed;
        }
        void ChangeInvertionState_Elapsed(object sender, ElapsedEventArgs e)
        {
            foreach (SketchPad item in containsSelectedItems)
            {
                item.UseReversedColor = !item.UseReversedColor;
                item.Canvas.Invalidate();
            }
        }
        void AutoSave_Elapsed(object sender, ElapsedEventArgs e)
        {
            lvDocuments.CrossThreadInvoke(() =>
            {
                foreach (ListViewItem item in lvDocuments.Items)
                {
                    Document doc = item.Tag as Document;
                    if (doc == null) continue;
                    FileStream tmpFile;
                    try
                    {
                        tmpFile = new FileStream(Flags.TempPath + $"\\{doc.Title} {DateTime.Now.ToString("yyyy_MM_dd HH_mm_ss")}{Flags.TempDocumentSuffix}", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                    //保存前不解除Timer的事件可能会引发此窗口未标记为可序列化异常
                    AutoSave.Elapsed -= AutoSave_Elapsed;
                    if (doc.SilentSave(tmpFile))
                    {
                        Program.WinMain.ShowMainStatus(string.Format(Resources.Translate("DocumentAutoSave"), doc.Title, DateTime.Now));
                    }
                    AutoSave.Elapsed += AutoSave_Elapsed;
                    tmpFile.Close();
                }
            });
        }
        public void ChangeColorTheme(UIColorTheme theme)
        {
            switch (theme)
            {
                case UIColorTheme.Light:
                    EnableVSRenderer(VisualStudioToolStripExtender.VsVersion.Vs2015, vS2015LightTheme1);
                    break;
                case UIColorTheme.Blue:
                    EnableVSRenderer(VisualStudioToolStripExtender.VsVersion.Vs2015, vS2015BlueTheme1);
                    break;
                case UIColorTheme.Dark:
                    EnableVSRenderer(VisualStudioToolStripExtender.VsVersion.Vs2015, vS2015DarkTheme1);
                    break;
            }
        }
        void EnableVSRenderer(VisualStudioToolStripExtender.VsVersion version, ThemeBase theme)
        {
            visualStudioToolStripExtender1.SetStyle(toolStrip2, version, theme);
            visualStudioToolStripExtender1.SetStyle(cmsList, version, theme);
            visualStudioToolStripExtender1.SetStyle(cmsItem, version, theme);
        }
        private void tsbNew_Click(object sender, EventArgs e)
        {
            AddDocument();
        }
        public bool AddDocument()
        {
            AddDocumentWindow ad = new AddDocumentWindow();
            if (ad.ShowDialog() == DialogResult.OK)
            {
                Document doc = CreateDocument(ad.DocumentTitle);
                doc.Remark = ad.Remark;
                doc.LinkedFile = ad.LinkedFile;
                AddSketchpad();
                doc.Modified = false;
                return true;
            }
            return false;
        }
        private void tsbOpen_Click(object sender, EventArgs e)
        {
            OpenDocuments();
        }
        public bool OpenDocuments()
        {
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                return OpenDocuments(ofd.FileNames) == ofd.FileNames.Length;
            }
            return false;
        }
        public int OpenDocuments(IEnumerable<string> paths)
        {
            StringBuilder sb = new StringBuilder();
            IFormatter fmt = new BinaryFormatter();
            int count = 0;
            foreach (string item in paths)
            {
                if (checkExist(item))
                {
                    sb.AppendLine(item);
                    continue;
                }
                Document doc = openDocument(item, fmt);
                if (doc == null) continue;
                lvDocuments.Items.Add(doc.DocumentsManagerItem);
                Program.WinMain.WindowTopMenu.DropDownItems.Insert(Program.WinMain.WindowTopMenu.DropDownItems.Count - 1, doc.WindowMenuItem);
                doc.Activate();
                count++;
            }
            if (sb.Length > 0)
            {
                MessageBox.Show($"{Resources.Translate("FileItemExist")}:{Environment.NewLine}{sb}", Resources.Translate("Prompt"), MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            return count;
        }
        public Document OpenDocument(string path)
        {
            if (checkExist(path))
            {
                MessageBox.Show(string.Format(Resources.Translate("DocumentOpened"), path), Resources.Translate("Prompt"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                return null;
            }
            Document doc = openDocument(path, new BinaryFormatter());
            if (doc == null) return null;
            lvDocuments.Items.Add(doc.DocumentsManagerItem);
            Program.WinMain.WindowTopMenu.DropDownItems.Insert(Program.WinMain.WindowTopMenu.DropDownItems.Count - 1, doc.WindowMenuItem);
            doc.Activate();
            return doc;
        }
        Document openDocument(string path, IFormatter fmt)
        {
            object[] objs;
            FileStream stream;
            try
            {
                stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
                objs = fmt.Deserialize(stream) as object[];
            }
            catch (SerializationException)//隐藏序列化
            {
                MessageBox.Show(Resources.Translate("OpenDocumentFailed"), Resources.Translate("Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Resources.Translate("Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
            Document doc = objs[0] as Document;
            doc.AfterDeserialized(objs[1] as Dictionary<string, object>);
            doc.AfterSaved();
            doc.LinkedFile = stream;
            return doc;
        }
        bool checkExist(string fileName)
        {
            foreach (ListViewItem item in lvDocuments.Items)
            {
                Document doc = item.Tag as Document;
                if (doc.LinkedFile != null && doc.LinkedFile.Name.Equals(fileName, StringComparison.OrdinalIgnoreCase))
                {
                    return true;
                }
            }
            return false;
        }

        public void lvDocuments_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvDocuments.SelectedItems.Count > 0)
            {
                //SelectedDocument?.SelectedPad?.Layer.SelectNone();
                Text = $"{Resources.JoinLocalizedString("Document", "Manager")} - " + (lvDocuments.SelectedItems.Count == 1 ? lvDocuments.SelectedItems[0].Text : $"({Resources.JoinLocalizedString("Multiple", "Document")})");
                Document[] docs = new Document[lvDocuments.SelectedItems.Count];
                bool enableDocumentOperation = false;
                for (int i = 0; i < docs.Length; i++)
                {
                    docs[i] = lvDocuments.SelectedItems[i].Tag as Document;
                    if (docs[i].DocumentWindow.DockState != DockState.Unknown && docs[i].DocumentWindow.DockState != DockState.Hidden)
                    {
                        enableDocumentOperation = true;
                    }
                }
                tsbSave.Enabled = tsbSaveAs.Enabled = tsbPrint.Enabled = tsbNewPad.Enabled = tsbCloseFile.Enabled = enableDocumentOperation;
                Program.WinProperty.PropertyGrid.SelectedObjects = docs;
                Program.WinProperty.Text = $"{Resources.Translate("Property")} - " + (lvDocuments.SelectedItems.Count == 1 ? lvDocuments.SelectedItems[0].Text : $"({Resources.JoinLocalizedString("Multiple", "Document")})");
                lvDocuments.ContextMenuStrip = cmsItem;
            }
            else
            {
                tsbSave.Enabled = tsbSaveAs.Enabled = tsbPrint.Enabled = tsbNewPad.Enabled = tsbCloseFile.Enabled = false;
                Text = Resources.JoinLocalizedString("Document", "Manager");
                lvDocuments.ContextMenuStrip = cmsList;
                if (Program.WinProperty.PropertyGrid.SelectedObjects != null &&
                    Program.WinProperty.PropertyGrid.SelectedObjects.All((object obj) =>
                {
                    return obj is Document;
                }))
                {
                    Program.WinProperty.PropertyGrid.SelectedObjects = null;
                    Program.WinProperty.Text = Resources.Translate("Property");
                }
            }
        }

        private void lvDocuments_ItemActivate(object sender, EventArgs e)
        {
            Document doc = lvDocuments.SelectedItems[0].Tag as Document;
            if (doc != null)
            {
                doc.Activate();
            }
        }
        public ListView DocumentList
        {
            get { return lvDocuments; }
        }
        private void tsbCloseFile_Click(object sender, EventArgs e)
        {
            try
            {
                CloseDocument();
            }
            catch (Exception)
            {

            }
        }
        public Document[] CloseDocument(bool all = false, bool remove = false, bool delFile = false)
        {
            return CloseDocument(lvDocuments.GetItems(all).Select((ListViewItem item) => { return item.Tag as Document; }), remove, delFile);
        }
        /// <summary>
        /// 回滚文档（删除所有修改）
        /// </summary>
        /// <param name="doc">要回滚的文档</param>
        /// <returns></returns>
        public Document RollBack(Document doc)
        {
            if (doc.LinkedFile == null) return null;
            doc.LinkedFile.Close();
            Document old = openDocument(doc.LinkedFile.Name, new BinaryFormatter());
            if (old == null) return null;
            lvDocuments.Items.Insert(doc.DocumentsManagerItem.Index, old.DocumentsManagerItem);
            doc.DocumentsManagerItem.Remove();
            int index = Program.WinMain.WindowTopMenu.DropDownItems.IndexOf(doc.WindowMenuItem);
            Program.WinMain.WindowTopMenu.DropDownItems.Insert(index, old.WindowMenuItem);
            Program.WinMain.WindowTopMenu.DropDownItems.RemoveAt(index + 1);
            old.Activate();
            //old.DocumentWindow.DockState = doc.DocumentWindow.DockState;
            doc.DocumentWindow.Close();
            return old;
        }
        public Document[] CloseDocument(IEnumerable<Document> docs, bool remove = false, bool delFile = false)
        {
            List<Document> closed = new List<Document>();
            foreach (Document doc in docs)
            {
                Document target = doc;
                if (target.Modified)
                {
                    switch (MessageBox.Show(string.Format(Resources.Translate("SaveAsk"), target.LinkedFile == null ? target.Title : target.LinkedFile.Name), Resources.Translate("Ask"), MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question))
                    {
                        case DialogResult.Yes:
                            target.BeforeSaving();
                            target.Save();
                            target.AfterSaved();
                            break;
                        case DialogResult.No:
                            if (target.LinkedFile == null)
                            {
                                remove = true;
                            }
                            else
                            {
                                target = RollBack(target);
                            }
                            break;
                        case DialogResult.Cancel:
                            OperationCanceledException ex = new OperationCanceledException();
                            ex.Data.Add("Closed", closed.ToArray());
                            throw ex;
                    }
                }
                else
                {
                    if (target.LinkedFile == null)
                    {
                        remove = true;
                    }
                }
                if (remove)
                {
                    target.DocumentWindow.Close();
                    target.DocumentsManagerItem.Remove();
                    if (delFile)
                    {
                        try
                        {
                            File.Delete(target.LinkedFile.Name);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, Resources.Translate("Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                            continue;
                        }
                    }
                }
                else
                {
                    target.DocumentWindow.DockState = DockState.Hidden;
                }
                closed.Add(doc);
            }
            return closed.ToArray();
        }
        private void tsbSave_Click(object sender, EventArgs e)
        {
            Save();
        }
        public bool Save(bool all = false)
        {
            return Save(lvDocuments.GetItems(all).Select((ListViewItem item) => { return item.Tag as Document; }));
        }
        public bool Save(IEnumerable<Document> docs)
        {
            bool success = true;
            foreach (Document doc in docs)
            {
                doc.BeforeSaving();
                if (doc.Save())
                {
                    UserOptions.Instance.AddRecentDocument(doc.Title, doc.LinkedFile.Name);
                    UserOptions.Instance.AutoSaveNewestPath.Remove(doc.GetHashCode().ToString());
                    UserOptions.Instance.AutoSaveNewestPath.Remove(doc.LinkedFile.Name);
                    UserOptions.Instance.Save();
                }
                else
                {
                    success = false;
                }
                doc.AfterSaved();
            }
            int index = 0;
            ToolStripItemCollection col = Program.WinMain.RecentOpenedDocumentMenu.DropDownItems;
            col.Clear();
            foreach (Tuple<string, string> doc in UserOptions.Instance.RecentDocuments)
            {
                ToolStripMenuItem item = new ToolStripMenuItem($"&{index++} {doc.Item1} [{doc.Item2}]", null, Program.WinMain.RecentItem_Click) { Tag = doc };
                col.Add(item);
            }
            Program.WinMain.RecentOpenedDocumentMenu.Enabled = col.Count > 0;
            return success;
        }
        public void AddSketchpad(IEnumerable<Document> docs)
        {
            foreach (Document item in docs)
            {
                AddSketchpadWindow ap = new AddSketchpadWindow(item.CheckPadNameExist, item.Title);
                if (ap.ShowDialog() == DialogResult.OK)
                {
                    SketchPad pad = item.CreateSketchpad(ap.PadName, ap.CoordinateType);
                    pad.RegisterPadEvents();
                    pad.Activate();
                }
            }
        }
        public void AddSketchpad()
        {
            if (lvDocuments.SelectedItems.Count == 0)
            {
                MessageBox.Show(Resources.Translate("SelectDocumentFirst"), Resources.Translate("Exclamation"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                AddSketchpad(lvDocuments.SelectedItems.OfType<ListViewItem>().Select((ListViewItem item) => { return item.Tag as Document; }));
            }
        }
        private void tsbNewPad_Click(object sender, EventArgs e)
        {
            AddSketchpad();
        }
        private void tsbSaveAs_Click(object sender, EventArgs e)
        {
            SaveAs();
        }
        public void SaveAs(bool all = false)
        {
            IFormatter fmt = new BinaryFormatter();
            foreach (ListViewItem item in lvDocuments.GetItems(all))
            {
                Document doc = item.Tag as Document;
                sfd.Title = $"\"{doc.Title}\"{Resources.Translate("SaveAs")}";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    doc.BeforeSaving();
                    doc.SaveAs(sfd.FileName);
                    doc.AfterSaved();
                }
            }
        }
        private void lvDocuments_SizeChanged(object sender, EventArgs e)
        {
            lvDocuments.Columns[0].Width = (int)((lvDocuments.Width - 25) * 0.4);
            lvDocuments.Columns[1].Width = lvDocuments.Width - lvDocuments.Columns[0].Width - 50;
        }
        public void Float(bool all = false)
        {
            foreach (ListViewItem item in lvDocuments.GetItems(all))
            {
                Document doc = item.Tag as Document;
                doc.DocumentWindow.DockState = DockState.Float;
            }
        }
        private void tsbPrint_Click(object sender, EventArgs e)
        {
            Print();
        }
        public void Print(bool all = false)
        {
            List<SketchPad> pads = new List<SketchPad>();
            foreach (ListViewItem item in lvDocuments.GetItems(all))
            {
                Document doc = item.Tag as Document;
                pads.AddRange(doc.Pads);
            }
            PrintSketchpadsWindow psw = new PrintSketchpadsWindow(pads);
            if (psw.ShowDialog() == DialogResult.OK)
            {
                foreach (SketchPad item in psw.CheckedSketchpads)
                {
                    item.Print(Program.WinMain.DefaultPageSettings, Program.WinMain.DefaultPrinterSettings);
                }
            }
        }
        private void tsbSave_EnabledChanged(object sender, EventArgs e)
        {
            Program.WinMain.NotifyMenuPropertyChanged("tsmiSave", "Enabled", tsbSave.Enabled);
        }

        private void tsbSaveAs_EnabledChanged(object sender, EventArgs e)
        {
            Program.WinMain.NotifyMenuPropertyChanged("tsmiSaveAs", "Enabled", tsbSaveAs.Enabled);
        }

        private void tsbNewPad_EnabledChanged(object sender, EventArgs e)
        {
            Program.WinMain.NotifyMenuPropertyChanged("tsmiNewPad", "Enabled", tsbNewPad.Enabled);
        }

        private void tsbPrint_EnabledChanged(object sender, EventArgs e)
        {
            Program.WinMain.NotifyMenuPropertyChanged("tsmiPageSettings", "Enabled", tsbPrint.Enabled);
            Program.WinMain.NotifyMenuPropertyChanged("tsmiViewPrint", "Enabled", tsbPrint.Enabled);
            Program.WinMain.NotifyMenuPropertyChanged("tsmiPrint", "Enabled", tsbPrint.Enabled);
        }

        private void tsbCloseFile_EnabledChanged(object sender, EventArgs e)
        {
            Program.WinMain.NotifyMenuPropertyChanged("tsmiCloseDocument", "Enabled", tsbCloseFile.Enabled);
        }

        private void 新建文档NToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddDocument();
        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenDocuments();
        }

        private void 全部保存SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Save(true);
        }

        private void 添加画板ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddSketchpad();
        }

        private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Print();
        }

        private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveAs();
        }

        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void 关闭文档CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                CloseDocument();
            }
            catch (Exception)
            {

            }
        }

        private void DocumentsManagerWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                DockState = DockState.Hidden;
                e.Cancel = true;
            }
            Program.WinMain.DocumentManagerMenu.Checked = false;
        }
        private void 移除RToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                CloseDocument(false, true);
            }
            catch (Exception)
            {

            }
        }

        private void 移除并删除源文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                CloseDocument(false, true, true);
            }
            catch (Exception)
            {

            }
        }
        private void DocumentsManagerWindow_Load(object sender, EventArgs e)
        {
            AutoSave.Start();
        }
    }
}
